package com.csp.mingyue.common.sms.core;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.csp.mingyue.common.sms.exception.SmsException;
import com.csp.mingyue.common.sms.support.SmsProperties;
import com.csp.mingyue.common.sms.vo.SmsResult;
import lombok.SneakyThrows;

import java.util.Map;

/**
 * Aliyun 短信模板
 *
 * @author njy
 * @date 2023/8/25 10:07
 */
public class AliyunSmsTemplate implements SmsTemplate {

	private SmsProperties properties;

	private Client client;

	@SneakyThrows(Exception.class)
	public AliyunSmsTemplate(SmsProperties smsProperties) {
		this.properties = smsProperties;
		Config config = new Config()
				// 您的 AccessKey ID
				.setAccessKeyId(smsProperties.getAccessKeyId())
				// 您的 AccessKey Secret
				.setAccessKeySecret(smsProperties.getAccessKeySecret())
				// 访问的域名
				.setEndpoint(smsProperties.getEndpoint());
		this.client = new Client(config);
	}

	public SmsResult send(String phones, String templateId, Map<String, String> param) {
		if (StrUtil.isBlank(phones)) {
			throw new SmsException("手机号不能为空");
		}
		if (StrUtil.isBlank(templateId)) {
			throw new SmsException("模板ID不能为空");
		}
		SendSmsRequest req = new SendSmsRequest().setPhoneNumbers(phones).setSignName(properties.getSignName())
				.setTemplateCode(templateId).setTemplateParam(JSONUtil.toJsonStr(param));
		try {
			SendSmsResponse resp = client.sendSms(req);
			return SmsResult.builder().isSuccess("OK".equals(resp.getBody().getCode()))
					.message(resp.getBody().getMessage()).response(JSONUtil.toJsonStr(resp)).build();
		}
		catch (Exception e) {
			throw new SmsException(e.getMessage());
		}
	}

}
